package com.example.android_29;

import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

public class FirstActivity extends AppCompatActivity {
    private static final String TAG = "FirstActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //加载布局,纯静态页面
        setContentView(R.layout.layout_first);
        Log.d(TAG, "onCreate: ");
        //注册button活动
        Button
                button = (Button) findViewById(R.id.button_0),
                button1 = (Button) findViewById(R.id.button_2),
                button2 = (Button) findViewById(R.id.button_7);
        button.setOnClickListener(
                new View.OnClickListener(){
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(FirstActivity.this, "you clicked button 0", Toast.LENGTH_SHORT).show();
                    }
                }
                //Lambda表达式
                //v-> Toast.makeText(FirstActivity.this,"you clicked Button 0",Toast.LENGTH_SHORT).show()

        );
        button1.setOnClickListener(
                new View.OnClickListener(){
                    @Override
                    public void onClick(View v) {
//                        startActivity(new Intent("com.example.sec").addCategory("com.example.second"));
                        Intent intent = new Intent("com.example.sec");
                        //区别
//                        intent.hasCategory("com.example.second");
//                        intent.addCategory("com.example.second");
                        intent.putExtra("extra_data","data from the FirstOrder");
                        //startActivity(intent);
                        startActivityForResult(intent,2);
                    }
                }
                //v-> startActivity(new Intent(FirstActivity.this,SecondActivity.class))
        );
        button2.setOnClickListener(
                new View.OnClickListener(){
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(FirstActivity.this,ThirdActivity.class);
                        startActivity(intent);
                    }
                }
        );

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main,menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()){
            case R.id.item_add:
                Toast.makeText(this,"You clicked Add",Toast.LENGTH_SHORT).show();
                break;
            case R.id.item_del:
                Toast.makeText(this,"You clicked Del",Toast.LENGTH_SHORT).show();
                break;
            default:
        }
        return true;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        switch (requestCode){
            case 2:
                if(resultCode == RESULT_OK){
                    Toast.makeText(this, "sec:"+data.getStringExtra("data_return"), Toast.LENGTH_SHORT).show();
                }
                break;
            default:
        }
    }


    @Override
    protected void onStart() {
        super.onStart();
        Log.d(TAG, "onStart: ");
        Log.e(TAG, "onStart: "+this.toString());
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d(TAG, "onResume: ");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d(TAG, "onPause: ");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.d(TAG, "onStop: ");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy: ");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.d(TAG, "onRestart: ");
    }
}
